<?php
/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/7/26
 * Time: 15:00
 */
require_once APPPATH.'third_party/PHPExcel/PHPExcel/IOFactory.php';
class Kdtstat extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('excelproc_model');
        $this->load->model('kdt_stat_model');
        $this->load->model('kdt_goods_model');
    }

    /**
     * 导出游戏引导师信息
     *
     */
    public function createGamerinfoExcel()
    {
        $start = $this->input->get('start');
        $end = $this->input->get('end');
        $start = strtotime($start);
        $end = strtotime($end);
        if (!isset($start)||!isset($end)) $this->error();

        $this->load->model('gamer_model');
        $data = $this->gamer_model->getAllGamerinfo($start,$end);
//        $this->excelproc_model->printSheet($data);

        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("gamer_info.xlsx");
        $this->success("gamer_info.xlsx comlpete");
    }
    public function createGamerTelExcel()
    {
        $start =  strtotime('2017-04-18 00:00:00');
        $end = strtotime('2017-05-05 00:00:00');

        $this->load->model('gamer_model');
        $data = $this->gamer_model->getGamertel($start,$end);
//        $this->excelproc_model->printSheet($data);

        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("gamer_tel.xlsx");
        $this->success("gamer_tel.xlsx comlpete");
    }
    //统计有赞已购用户的信息
    public function createKdtCustomerExcel()
    {
        $start = $this->input->get('start');
        $end = $this->input->get('end');
        if (!isset($start)||!isset($end)) $this->error();
        $start = strtotime($start);
        $end = strtotime($end);

        $this->load->model('kdt_customer_model');
        $data = $this->kdt_customer_model->getCustomers($start,$end);
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("kdt-custumers.xlsx");
        $this->success("kdt-custumers.xlsx comlpete");
    }
    //统计有赞销售员推文信息
    public function createSalesmanArticleExcel()
    {
        $this->load->model('kdtsalesman_model');
        $data = $this->kdtsalesman_model->getSalesmanArticleData();
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("kdt-salesman-article-data.xlsx");
        echo "kdt-salesman-article-data.xlsx comlpete".PHP_EOL;
    }
    //统计有赞交易数据
    public function createKdtTradesExcel($type="fee",$time="day",$mondays=31)
    {
        if (!is_cli()) die('request forbidden');
        $start=strtotime("2018-01-01 00:00:00");
        $end=strtotime("2019-01-01 00:00:00");
        $step=$time==="day"?24*3600:24*3600*(int)$mondays;
        $data = $this->kdt_stat_model->getKdtTrades($start,$end,$step);
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($type==="fee"?$data['fee']:$data['num'],'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);

        $date = date('Y-m-d', $start).'-'.date('Y-m-d', $end);
        $objw->save($type==="fee"?"kdt-sale-".$date.".xlsx":"kdt-salenum-".$date.".xlsx");
        echo "complete!".PHP_EOL;
    }
    //统计有赞代言人交易数据
    public function createSalesmanTradesExcel($type="fee",$time="day",$mondays=31)
    {
        if (!is_cli()) die('request forbidden');
        $start=strtotime("2018-01-01 00:00:00");
        $end=strtotime("2019-01-01 00:00:00");
        $step=$time==="day"?24*3600:24*3600*(int)$mondays;
        $data = $this->kdt_stat_model->getSalesmanTrades($start,$end,$step);
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($type==="fee"?$data['fee']:$data['num'],'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);

        $date = date('Y-m-d', $start).'-'.date('Y-m-d', $end);
        $objw->save($type==="fee"?"sm-sale-".$date.".xlsx":"sm-salenum-".$date.".xlsx");
        echo "complete!".PHP_EOL;
    }
    //展示有赞商品列表
    public function showGoodsList()
    {
        if (!is_cli()) die('request forbidden');
        $this->load->model('kdt_api_model');
        $this->kdt_api_model->getGoodsInfo();
    }
    public function synTrades()
    {
        if (!is_cli()) die('request forbidden');
        $start = "2018-1-1 00:00:00";
        $end = "2019-1-1 00:00:00";
        $this->kdt_stat_model->synTrades($start,$end);
    }
    //同步销售员订单数据
    public function synSalesmanTrades()
    {
        if (!is_cli()) die('request forbidden');
        $start = "2018-1-1 00:00:00";
        $end = "2019-1-1 00:00:00";
        $this->kdt_stat_model->synSalesmanTrades($start,$end);
    }
    public function synSalesmanTradesByPhone()
    {
        if (!is_cli()) die('request forbidden');
        $phones = array(
            '13514317919','13514317919','18621853523','18621853523','18601701616',
            '13830067389','18221995526','15913144561','15913144561','13916422689',
            '15103275982','18913718165','18913718165','13564622950','13849908396',
            '15099410103','15099410103','15099410103','13409929131','13409929131',
            '15195992005','18861507887','18861507887','18861507887','13770639468',
            '13770639468','18351990856','13816390690');
        $start = "2018-06-18 00:00:00";
        $end = "2018-08-02 00:00:00";
        $this->kdt_stat_model->synSalesmanTradesByPhone($phones,$start,$end);
    }
    //同步销售员列表
    public function synSalesmanList()
    {
        if (!is_cli()) die('request forbidden');
        $this->kdt_stat_model->synSalesmanList();
    }
    //有赞订单地域分布
    public function createTradesByCityExcel()
    {
        if (!is_cli()) die('request forbidden');
        $data = $this->kdt_stat_model->getTradesByCityData();
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("trade-city-data.xlsx");
        echo "complete!".PHP_EOL;
    }
    public function createTradesByProvinceExcel()
    {
        if (!is_cli()) die('request forbidden');
        $data = $this->kdt_stat_model->getTradesByProvinceData();
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("trade-city-province.xlsx");
        echo "complete!".PHP_EOL;
    }
    public function createEvaluationExcel()
    {
        if (!is_cli()) die('request forbidden');
        $uidarr = array(
            2623,2427,9373,11661,12730,1853,122,29,2406,7163,
            4763,3933,12686,7929,3370,4881,12712,12711,22,9701,
            12931,9351,1077,8,9,882,12929,575,9095,730,
            10954,986,92,512,9485,10050,10576,10,5223,8447,
            7950,5537,36,5247,3,7917,745,2473);
        $this->load->model('redis_model');
        $redis = $this->redis_model->getRedisInstance();
        $header=array('姓名','追求快乐取向','投入取向','追求意义取向','胜利取向');
        $data[] = $header;
        foreach ($uidarr as $u){
            $iscomplished = $redis->getBit('adv:evaluation:complished:2',$u);
            if ($iscomplished === 1 ){
                $redis->sAdd('adv:evaluation:complishedset:2',$u);
                $test2 = $redis->get('adv:evaluation:data:2:'.$u);
                $test2score = array();
                $username = $redis->get('adv:evaluate:username:'.$u);
                $test2score[0] = $username;
                $test2score[1] = 0;
                $test2score[2] = 0;
                $test2score[3] = 0;
                $test2score[4] = 0;
                if ($test2!==false){
                    $test2 = igbinary_unserialize($test2);
                    foreach ($test2 as $ans){
                        switch ((int)$ans['type']){
                            case 1:
                                $test2score[1]+=(int)$ans['ans'];
                                break;
                            case 2:
                                $test2score[2]+=(int)$ans['ans'];
                                break;
                            case 3:
                                $test2score[3]+=(int)$ans['ans'];
                                break;
                            case 4:
                                $test2score[4]+=(int)$ans['ans'];
                                break;
                        }
                    }
                }
                $data[] = $test2score;
            }
        }
        $obj = new PHPExcel();
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("qf-evaluation.xlsx");
        echo "qf-evaluation.xlsx complete!".PHP_EOL;
    }
}